import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { existsSync } from 'node:fs';
import { cac } from 'cac';
import { log } from './utils';
import { start } from './compiler';
import { VERSION } from './const';
import type { GlobalCLIOptions } from './types';

const _dirname = dirname(fileURLToPath(import.meta.url));
const cli = cac('stsc');

cli.option('-c, --config <file>', `[string] use specified config file`);

cli
  .command('', '开始解析')
  .alias('parse')
  .action((options: GlobalCLIOptions) => {
    debugger;
    let configPath = resolve(_dirname, options.c || options.config || './config/config.ts');

    if (!existsSync(configPath)) {
      configPath = resolve(_dirname, 'config.ts');
    }
    if (!existsSync(configPath)) {
      log.error('解析异常', '没有找到配置文件');
      return;
    }
    // @ts-ignore
    global.__stsc_config_path = configPath;
    start();
  });
cli.help();
cli.version(VERSION);
cli.parse();
